package service;

        import dao.BillDao;
        import dao.WorkerDao;
        import dao.impl.BillDaoImpl;
        import dao.impl.WorkerDaoImpl;
        import org.apache.commons.beanutils.BeanUtils;
        import pojo.Bill;
        import pojo.Worker;
        import util.LogUtil;
        import util.MD5Util;
        import util.Result;

        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import java.lang.reflect.InvocationTargetException;
        import java.util.List;
        import java.util.Map;

public class BillService {

    private BillDao billDao = new BillDaoImpl();
    private WorkerDao workerDao = new WorkerDaoImpl();

    /**
     * 根据条件查找所有账单
     * @param request
     * @param response
     */
    public void findBillByParam(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Bill bill = new Bill();
            BeanUtils.populate(bill, parameterMap);
            List<Bill> billByParam = billDao.findBillByParam(bill);
            if (billByParam==null){
                Result.response(1001, "获取失败！", response);
                return;
            }
            Result.response(0, "获取成功！", billDao.countFindBillByParam(bill), response, billByParam);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    /**
     * 添加账单
     * @param request
     * @param response
     */
    public void addBill(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Worker worker = new Worker();
            Bill bill = new Bill();
            BeanUtils.populate(worker, parameterMap);
            BeanUtils.populate(bill, parameterMap);
            if (worker==null || bill==null){
                Result.response(1001, "参数有误！", response);
                return;
            }
            Worker workerById = workerDao.findWorkerById(worker.getWorkerId().toString());
            if (!workerById.getWorkerPassword().equals(MD5Util.MD55(worker.getWorkerPassword()))) {
                Result.response(1002, "密码错误！", response);
                return;
            }
            bill.setBillAction(bill.getBillAction()+" 操作员:"+worker.getWorkerName());
            if (bill.getBillType() == 0){
                bill.setBillRest(billDao.findBillRest()-bill.getBillMoney());
            }else if (bill.getBillType() == 1){
                bill.setBillRest(billDao.findBillRest()+bill.getBillMoney());
            }
            if (billDao.addBill(bill)<=0) {
                LogUtil.addLog("添加一笔账单信息失败", request,1);
                Result.response(1003, "添加失败！！", response);

                return;
            }
            LogUtil.addLog("添加了一笔账单信息", request);
            Result.response(200, "添加成功！！", response);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
